package tree.binary;

import entity.tree.TreeNode;

public class leetcode543二叉树的直径 {
    int max = Integer.MIN_VALUE;
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return max;
    }

    public int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = dfs(root.left);
        int rightHeight = dfs(root.right);
        max = Integer.max(leftHeight + rightHeight, max);
        return 1 + Integer.max(leftHeight, rightHeight);
    }
}
